<?php

if (isset($_POST['submit'])) {
    /* get information from POST method */
    if (($_POST["manu"]) == "-1")
        echo("<h4>*Chưa chọn nhà sản xuất");
    else if (($_POST['model']) == NULL)
        echo("<h4>*Chưa chọn tên model</h4>");
    else if (($_POST['price']) == NULL)
        echo("<h4>*Mời nhập vào giá bán </h4>");
    else if (($_POST['gia_mua']) == NULL)
        echo ("<h4>Mời nhập giá mua</h4>");
    else if (($_POST['qty']) == NULL)
        echo("<h4>*Mời nhập vào sô lượng </h4>");
    else if (($_POST['screen']) == NULL)
        echo("<h4>*Mời nhập vào kích cỡ màn hình</h4>");
    else if (($_POST['appearance']) == NULL)
        echo("<h4>*Chọn kiểu dáng</h4>");
    else if (($_POST['battery']) == NULL)
        echo("<h4>*Chọn kiểu pin</h4>");
    else if ($_FILES['image']['error'] > 0)
        echo("<h4>*File ảnh bị lỗi!</h4>");
    else {
        $manu = $_POST['manu'];
        $name = $_POST["model"];
        $price = $_POST["price"];
        $entertainment = $_POST["entertainment"];
        $os = ($_POST['os'] == NULL) ? "Không" : $_POST['os'];
        $network = $_POST['network'];
        if ($_POST['GPRS'] != 'off')
            $network .= "GPRS ,";
        if ($_POST['Wireless'] != 'off')
            $network .= " Wi-fi ,";
        if ($_POST['3G'] != 'off')
            $network .= " 3G ,";
        if ($_POST['Bluetooth'] != 'off')
            $network .= " Bluetooth";
        if ($network == NULL)
            $network = "Không";
        $warranty = ($_POST['warranty'] == NULL) ? "Không" : $_POST['warranty'];
        $promotion = ($_POST['promotion'] == NULL) ? "Không" : $_POST['promotion'];
        $screen = $_POST["screen"];
        $appr = $_POST["appearance"];
        $battery = $_POST["battery"];
        $qty = $_POST['qty'];
        move_uploaded_file($_FILES['image']['tmp_name'], "picture/" . $_FILES['image']['name']);
        $url = "picture/" . $_FILES['image']['name'];

        /* Connect to database, in this case, connect to localhost  */
        include_once('conf/dbconfig.php');
        $cfg = new dbconfig();
        $host = $cfg->get_dbhost();
        $db = $cfg->get_dbname();
        $usr = $cfg->get_dbusername();
        $pass = $cfg->get_dbpwd();
        $connection = pg_connect("host = $host dbname = $db user = $usr password = $pass")
                or die("Cannot connect to database server: " . pg_last_error());
        /* define queries to search  */
        /* add data to table */
        $queryid = "Select mobileid from mobilephone";
        $idnumber = pg_num_rows(pg_query($connection, $queryid));
        $mobileid = strval($idnumber + 1);
        while (strlen($mobileid) < 4)
            $mobileid = "0" . $mobileid;

        $query = "INSERT INTO mobilephone(mobileid,model,price,quantity,entertainment,os,screen,appearance,battery,warranty,promotions,network,picture_path)
							VALUES ('$mobileid','$name',$price,$qty,'$entertainment' ,'$os','$screen','$appr','$battery','$warranty','$promotion','$network','$url')";
        $gia_mua = $_POST['gia_mua'];
        $query_1 = "select * from manufacturer where name = '$manu'";
        $result = pg_query($connection, $query_1);
        $manuid = pg_fetch_result($result, 0, 'manuid');
        $query_1 = "select * from provides";
        $result = pg_query($connection, $query_1);
        $provid = pg_num_rows($result);
        $provid = strval($provid + 1);
        while (strlen($provid) < 3)
            $provid = "0" . $provid;
        $provid = "P" . $provid;
        $today = getdate();
        $time = "";
        $time .= $today['year'] . "-" . $today['mon'] . "-" . $today['mday'];

        $query_1 = "insert into provides (provid, manuid, mobileid, quantity, price, date)
                        values ('$provid', '$manuid', '$mobileid', $qty, $gia_mua, '$time')";
//echo $query;
        $result = pg_query($connection, $query);
        $result = pg_query($connection, $query_1);
        if ($result != null)
            echo "Nhập kho thành công! </h4>";
        else
            echo "Có lỗi khi nhập kho, xin vui lòng thử lại sau!</h4>";
    }
}
?>